package com.funny.rabbitmq.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 延时消息
 */
@Slf4j
@RestController
@RequestMapping("/send/message")
public class SendMessageController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping(value = "/ttl/{message}")
    public String sendMessageTtl(@PathVariable String message) {
        log.info("发送信息到 2个ttl队列 date={}, message={}", new Date().toString(), message);

        rabbitTemplate.convertAndSend("X", "XA", "ttl为10s的队列:" + message);
        rabbitTemplate.convertAndSend("X", "XB", "ttl为40s的队列:" + message);

        return null;
    }

    @GetMapping(value = "/ttl/expire/{message}/{time}")
    public String sendMessageTtl(@PathVariable String message, @PathVariable String time) {
//        log.info("发送信息到 2个ttl队列 date={}, message={} time={}", new Date().toString(), message, time);

        rabbitTemplate.convertAndSend("X", "XC", "ttl为10s的队列:" + message, msg -> {
            msg.getMessageProperties().setExpiration(time);
            return msg;
        });

        return null;
    }
}
